local skel = fk.CreateSkill {
  name = "rmt__huangwu",
}

skel:addEffect(fk.GameStart, {
  anim_type = "drawcard",
  derived_piles = "$rmt__star",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:addToPile("$rmt__star", room:getNCards(7), true, skel.name)
    if player.dead or player:isKongcheng() or #player:getPile("$rmt__star") == 0 then return false end

    local cids = room:askToArrangeCards(player, {
      skill_name = skel.name,
      card_map = {
        player:getPile("$rmt__star"), player:getCardIds("h"),
        "$rmt__star", "$Hand"
      },
      prompt = "#rmt__huangwu-exchange",
      free_arrange = true,
    })
    room:swapCardsWithPile(player, cids[1], cids[2], skel.name, "$rmt__star")
  end,
})

skel:addEffect(fk.AfterDrawNCards, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    return target == player and not player:isKongcheng() and #player:getPile("$rmt__star") > 0
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#rmt__huangwu-exchange"})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cids = room:askToArrangeCards(player, {
      skill_name = skel.name,
      card_map = {
        player:getPile("$rmt__star"), player:getCardIds("h"),
        "$rmt__star", "$Hand"
      },
      prompt = "#rmt__huangwu-exchange",
      free_arrange = true,
    })
    room:swapCardsWithPile(player, cids[1], cids[2], skel.name, "$rmt__star")
  end,
})

skel:addEffect(fk.TurnEnd, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    local mark = player:getMark("rmt__huangwu-turn")
    return type(mark) == "string" and #table.filter(player:getPile("$rmt__star"), function (id)
      return Fk:getCardById(id).trueName == "slash"
    end) > tonumber(mark)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askToChoosePlayers(player, {
      targets = room.alive_players,
      max_num = 1, min_num = 1, skill_name = skel.name, cancelable = false,
      prompt = "#rmt__huangwu-damage",
    })
    room:damage { from = player, to = tos[1], damage = 1, skillName = skel.name, damageType = fk.FireDamage }
  end,
})

-- 回合开始时，记录此时星数量
skel:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "rmt__huangwu-turn", tostring(#table.filter(player:getPile("$rmt__star"), function (id)
      return Fk:getCardById(id).trueName == "slash"
    end)))
  end,
})

Fk:loadTranslationTable{
  ["rmt__huangwu"] = "荒武",
  [":rmt__huangwu"] = "锁定技，你视为拥有“七星”，你的“星”明置。每回合结束时，若“星”中的【杀】比回合开始时多，你对一名角色造成1点火焰伤害。",

  ["#rmt__huangwu-exchange"] = "荒武：你可以交换任意张“星”牌与手牌",
  ["#rmt__huangwu-damage"] = "荒武：选择一名角色，你对其造成1点火焰伤害",
  ["$rmt__star"] = "星",

  ["$rmt__huangwu1"] = "天生万物以养人，人无一物以报天。",
  ["$rmt__huangwu2"] = "状元百官都如狗，总是刀下觳觫材。",
}

return skel
